# -*- coding : utf-8 -*-
# coding: utf-8

import yaml
import os
import sqlite3Utils
import opexecl
import filetodb
import sys
import re
import chardet
import remote


class Op:
    def __init__(self):
        # py文件写法
        # with open(os.path.join(os.path.dirname(__file__), "keyvar.yaml"), 'r', encoding='utf-8') as f:
        # 打二进制包写法
        with open(os.path.join(os.getcwd(), "keyvar.yaml"), 'r', encoding='utf-8') as f:
            self.yamldict = yaml.load(f, Loader=yaml.Loader)

        # 初始化数据库
        filetodb.createtbl(self.yamldict['dbname'], self.yamldict['tblname'])
        # 如果有规则，初始化表，没有为空
        reg = os.listdir(os.path.join(os.getcwd(), 'temp'))
        if len(reg) == 0:
            pass
        elif len(reg) == 1 and reg[0].endswith('xml'):
            reg_filename = reg[0]
            # 获取规则list
            bigarray = filetodb.getReg(os.path.join(os.getcwd(), 'temp', reg_filename))
            flag = filetodb.inittbl(self.yamldict['dbname'], self.yamldict['tblname'], bigarray)
            if flag is True:
                os.remove(os.path.join(os.getcwd(), 'temp', reg_filename))
        else:
            print("temp目录下规则文件过多或规则文件不是xml格式, 请修改")
            sys.exit(99)

    def getfile(self) -> list:
        filelist = []
        method = self.yamldict['method']
        if method == 'remote':
            print("拉取远程文件")
            hostname = self.yamldict['hostname']
            port = self.yamldict['port']
            username = self.yamldict['username']
            password = self.yamldict['password']
            localpath = self.yamldict['localpath']
            r = remote.Remote(hostname, port, username, password)
            r.makepackage()
            r.downpackage(hostname, port, username, password, localpath)
            # 解压
            r.openpackage(localpath)
            # 遍历下载后目录
            for root, dirs, files in os.walk(self.yamldict['localpath'], topdown=True):
                for file in files:
                    if '.' in file:
                        if file.split('.')[1] in self.yamldict['filetype']:
                            with open(os.path.join(root, file), 'rb') as f:
                                tmpcode = chardet.detect(f.read())['encoding']
                                if tmpcode == 'utf-8' or tmpcode == 'ascii':
                                    filelist.append(os.path.join(root, file))
                    else:
                        continue
            print("处理以下文件: ", filelist)
        else:
            print("本地检查")
            for rootpath_items in self.yamldict['rootpath']:
                for root, dirs, files in os.walk(rootpath_items, topdown=True):
                    for file in files:
                        if '.' in file:
                            if file.split('.')[1] in self.yamldict['filetype']:
                                with open(os.path.join(root, file), 'rb') as f:
                                    tmpcode = chardet.detect(f.read())['encoding']
                                    if tmpcode == 'utf-8' or tmpcode == 'ascii':
                                        filelist.append(os.path.join(root, file))
                        else:
                            continue
            print("处理以下文件: ", filelist)

        return filelist

    def querykey(self):
        dbname = self.yamldict['dbname']
        tblname = self.yamldict['tblname']
        db = sqlite3Utils.MySqlite(dbname)
        sql = "CREATE TABLE IF NOT EXISTS {}(id INTEGER PRIMARY KEY AUTOINCREMENT, keys TEXT);".format(tblname)
        db.execute(sql)
        keydata = db.query(tblname)
        for keydict in keydata:
            for item in keydict:
                print(item, keydict[item], end="\t")
            print()

    def addkey(self, key):
        tblname = self.yamldict['tblname']
        dbname = self.yamldict['dbname']
        db = sqlite3Utils.MySqlite(dbname)
        sql = "CREATE TABLE IF NOT EXISTS {}(id INTEGER PRIMARY KEY AUTOINCREMENT, keys TEXT);".format(tblname)
        # 创建表
        db.execute(sql)
        keydata = db.query(tblname)
        tmpkey = [x for x in keydata if x['keys'] == key]
        if len(tmpkey) >= 1:
            print("您添加的非法关键词：", key, "已存在，添加失败")
        else:
            db.add(tblname, key)

    def delkey(self, key):
        tblname = self.yamldict['tblname']
        dbname = self.yamldict['dbname']
        db = sqlite3Utils.MySqlite(dbname)
        db.delete(tblname, key)
        print("删除非法关键词：", key, "成功")

    def scan(self, filelist):
        # 数据库content列表
        dbname = self.yamldict['dbname']
        tblname = self.yamldict['tblname']
        db = sqlite3Utils.MySqlite(dbname)
        datalist = db.query(tblname)

        plainlist = []
        regexplist = []
        for keydict in datalist:
            if keydict['type'] == 'plain/text':
                plainlist.append(
                    str(keydict['id']) + '==' + keydict['content'] + '==' + keydict['reason'] + '==' + keydict[
                        'type'] + '==' + keydict['note'])
            if keydict['type'] == 'regexp':
                regexplist.append(
                    str(keydict['id']) + '==' + keydict['content'] + '==' + keydict['reason'] + '==' + keydict[
                        'type'] + '==' + keydict['note'])

        # 遍历文件
        Biglist = []
        for file in filelist:
            # 定义行数为0
            count = 0
            # 遍历文本内容
            with open(file, mode='r', encoding='utf-8') as f:
                for line in f.readlines():
                    count += 1
                    # 文本
                    for itemp in plainlist:
                        if itemp.split('==')[1] in line:
                            Biglist.append((file, count, itemp.split('==')[1], itemp.split('==')[2],
                                            itemp.split('==')[3], itemp.split('==')[4], line))

                    # 正则
                    for itemr in regexplist:
                        regex = re.compile(itemr.split('==')[1])
                        num = regex.findall(line)
                        if len(num) > 0:
                            Biglist.append((file, count, itemr.split('==')[1], itemr.split('==')[2],
                                            itemr.split('==')[3], itemr.split('==')[4], line))

        return Biglist


if __name__ == '__main__':
    # parser = argparse.ArgumentParser(description="非法字符操作")
    # parser.add_argument("-v", "--verbose", help="查看非法字符", action="store_true")
    # parser.add_argument("-a", "--add", help="添加非法字符")
    # parser.add_argument("-d", "--delete", help="删除非法字符")
    # args = parser.parse_args()
    # if args.add:
    #     Op().addkey(args.add)
    # elif args.delete:
    #     Op().delkey(args.delete)
    # elif args.verbose:
    #     Op().querykey()
    # else:
    op = Op()
    myfile = op.getfile()
    print(myfile)
    lis = op.scan(myfile)
    opexecl.opexecl(lis)
